## rm(list = ls())

setwd("../../data/ANES/anes_mergedfile_2000to2004_dta")

library(foreign)
library(stargazer)
library(mediation)

DATA00 <- read.dta("anes_mergedfile_2000to2004.dta")

scaleIt <- function(x) {
    scale(x, center=median(x,na.rm=T))
}

DATA00$`Discusses politics (days in week)` <- as.numeric(DATA00$M001205)+1
## M001205    A3a. How often does R discuss politics
##    IF R DISCUSSES POLITICS:
##        How many days in the past week did you talk about
##        politics with family or friends?
##        ---------------------------------------------------------------------
##        1-7
##        96. NONE (NO DAYS)
##        98. DK
##        99. RF
##        00. NA; INAP, 5, 8, 9 in A3; no Post interview
DATA00$`Discusses politics (days in week)`[is.na(DATA00$`Discusses politics (days in week)`) & as.numeric(DATA00$M001204) == 2 & !is.na(as.numeric(DATA00$M001204))] <- rep(1, sum(as.numeric(DATA00$M001204) == 2 & is.na(DATA00$`Discusses politics (days in week)`), na.rm = T))
DATA00$`Discusses politics (days in week)`[is.na(DATA00$`Discusses politics (days in week)`) & as.numeric(DATA00$M001204) == 3 & !is.na(as.numeric(DATA00$M001204))] <- rep(0, sum(as.numeric(DATA00$M001204) == 3, na.rm = T))


DATA00$`Highest education` <- as.numeric(DATA00$M000913)
       ## 1. 8 grades or less and no diploma or equivalency
       ##    [0-8 in Y3, 5 in Y3a]
       ## 2. 9-11 grades, no further schooling (incl. 12 years
       ##    without diploma or equivalency) [9-12 in Y3, 5 in Y3a]
       ## 3. High school diploma or equivalency test [0-12 in Y3,
       ##    1 in Y3a]
       ## 4. More than 12 years of schooling, no higher degree
       ##    (13-17 in Y3, 96 in Y3b)
       ## 5. Junior or community college level degrees (AA degrees) (07 in Y3b)
       ## 6. BA level degrees; 17+ years, no advanced degree (01 in Y3b)
       ## 7. Advanced degree, including LLB [13-17 in Y3, 2-6 in Y3b]
       ## 8. DK   [98 in Y3, 8 in Y3a; 98 in Y3b]
       ## 9. NA  [99 in Y3; 9 in Y3a; 99 or NA in Y3b]

## DATA00$income <- scaleIt(as.numeric(DATA00$M000997))

DATA00$`Married` <- as.integer(as.numeric(DATA00$M000909) == 2)
       ## Are you married now and living with your (husband/wife)
       ## -- or are you widowed, divorced, separated, or have you
       ## never married?
       ## ---------------------------------------------------------------------

       ## Note: in cases 8,49,77,157,377,535,580,814,1087,1117,1202,1212,
       ## 1381,1639,1731 the partner/spouse identified in P000909 (marital
       ## status) was not a resident of the HU or was residing elsewhere
       ## on temporary basis. In cases 1281,1516,46,312,133,23,222 there
       ## is no information about spouse/partner.

       ## 1. MARRIED
       ## 2. WIDOWED
       ## 3. DIVORCED
       ## 4. SEPARATED
       ## 5. NEVER MARRIED
       ## 6. PARTNERED, NOT MARRIED [VOL]
       ## 8. DK
       ## 9. RF
       ## 0. NA
DATA00$`Never married` <- as.integer(as.numeric(DATA00$M000909) == 6)
DATA00$marital.status <- DATA00$M000909
levels(DATA00$marital.status) <- c(NA, "Married or partnered","Widowed","Divorced or separated","Divorced or separated","Never married", "Married or partnered",NA,NA)
DATA00$`Changed married` <- as.integer(DATA00$Married == 1 & as.numeric(DATA00$M045176) != 1)

DATA00$`Age` <- as.numeric(DATA00$M000908)
## actual age
DATA00$`Age^2` <- as.numeric(DATA00$M000908)^2

DATA00$`Interested in campaign` <- abs(as.numeric(DATA00$M000301)-5)
       ## Some people don't pay much attention to political
       ## campaigns. How about you? Would you say that you have
       ## been very much interested, somewhat interested or not
       ## much interested in the political campaigns so far this
       ## year?
       ## ---------------------------------------------------------------------

       ## 1. VERY MUCH INTERESTED
       ## 3. SOMEWHAT INTERESTED
       ## 5. NOT MUCH INTERESTED

       ## 8. DON'T KNOW
       ## 9. RF
       ## 0. NA

DATA00$`Changed party` <- with(DATA00, abs(as.numeric(M000523) - as.numeric(M045058x))>=4)
DATA00$`Changed party`[as.numeric(DATA00$M000523)>=8|as.numeric(DATA00$M000523)==4] <- NA
DATA00$`Changed party`[as.numeric(DATA00$M045058x)>=8] <- NA

DATA00$`Changed partyB` <- with(DATA00, abs(as.numeric(M000523) - as.numeric(M045058x))>=4|abs(as.numeric(M000523) - as.numeric(M045058x))>=3&as.numeric(M000523)%in%c(3,5)|abs(as.numeric(M000523) - as.numeric(M045058x))>=3&as.numeric(M045058x)%in%c(3,5))
DATA00$`Changed partyB`[as.numeric(DATA00$M000523)>=8|as.numeric(DATA00$M000523)==4] <- NA
DATA00$`Changed partyB`[as.numeric(DATA00$M045058x)>=8] <- NA

DATA00$`Changed party3` <- with(DATA00, abs(as.numeric(M000523) - as.numeric(M045058x))>=3)
DATA00$`Changed party3`[as.numeric(DATA00$M000523)>=8] <- NA
DATA00$`Changed party3`[as.numeric(DATA00$M045058x)>=8] <- NA

DATA00$`Changed party2` <- with(DATA00, abs(as.numeric(M000523) - as.numeric(M045058x))>=2)
DATA00$`Changed party2`[as.numeric(DATA00$M000523)>=8] <- NA
DATA00$`Changed party2`[as.numeric(DATA00$M045058x)>=8] <- NA

DATA00$`Changed party1` <- with(DATA00, abs(as.numeric(M000523) - as.numeric(M045058x))>=1)
DATA00$`Changed party1`[as.numeric(DATA00$M000523)>=8] <- NA
DATA00$`Changed party1`[as.numeric(DATA00$M045058x)>=8] <- NA

DATA00$`Moved` <- with(DATA00, as.integer(M045189x<=5|M045190x<=5)) #|as.integer(M023139)<=3 as.integer(M023139A)<=3 2000 ONLY
## These questions were only asked to new respondents in 2002.
## They imputed 2000 variables for old respondents).
##
## M045189x    Q271.f3z1x. Summary: How Long In Present Community
## POST-ELECTION SURVEY:
## QUESTION:
## ---------
## How long have you lived in your present community?
## VALID CODES:
## ------------
## 00. Less than 1 year
## 01. 12-18 months; 1 year
## 02. 19-24 months; 2 years
## 03. 3 years
## .
## .
## 76. 76 years or more
## MISSING CODES:
## --------------
## 88. Don't know
## 89. Refused
## 99. NA
## NOTES:
## ------
## If R answered 'all my life' then R's age in years has
## been coded 18-76.
##
## M045190x    Q275.f3z2x. Summary: How Long In Present Home
## POST-ELECTION SURVEY:
## QUESTION:
## ---------
## How long have you lived in your current home?
## VALID CODES:
## ------------
## 00. Less than 1 year
## 01. 12-18 months; 1 year
## 02. 19-24 months; 2 years
## 03. 3 years
## .
## .
## 76. 76 years or more
## MISSING CODES:
## --------------
## 88. Don't know
## 89. Refused
## 99. NA
## NOTES:
## ------
## If R answered 'all my life' then R's age in years has
## been coded 18-76.


DATA00$`Partisanship (left to right on 7-pt scale)` <- as.numeric(DATA00$M000523)
DATA00$`Partisanship (left to right on 7-pt scale)`[as.numeric(DATA00$M000523)>=8] <-NA
## M000523    K1x. Party ID summary
##        Generally speaking, do you think of yourself as a
##        Republican, a Democrat, an Independent, or what?
##        Would you call yourself a strong Democrat/Republican or
##        a not very strong Democrat/ Republican?
##        Do you think of yourself as closer to the Republican
##        Party or to the Democratic party?
##        Built from K1, K1a/b, K1c.
##        Code 8 (apolitical) was used only if the respondent had a
##        code of 5 (no preference) in K1 and a code of 3,8 or 9 in
##        K1c AND also showed little or no interest in politics in
##        response to the following survey questions:  PRE question
##        A1.(interest in campaigns), PRE question B1 (care about
##        Congressional race outcome), POST question C1 (voted), POST
##        question F5 (follow public affairs); respondents who
##        showed an interest in politics were coded 3.
##        0. Strong Democrat (1,1,0 in K1, K1a/b, K1c)
##        1. Weak Democrat (1,5/8/9,0 in K1, K1a/b, K1c)
##        2. Independent-Democrat (3/4/5/8,0,5 in K1,  K1a/b, K1c
##        3. Independent-Independent (3,0,3/8/9 in K1, K1a/b, K1c;
##           8,0,3 and 5,0,3/8/9 if not apolitical)
##        4. Independent-Republican (3/4/5/8,0,1 in K1, K1a/b, K1c)
##        5. Weak Republican (2,5/8/9,0 in K1, K1a/b, K1c)
##        6. Strong Republican (2,1,0 in K1, K1a/b, K1c)
##        7. Other; minor party; refuses to say (4,0,3/8/9 in K1, K1a/b, K1c)
##        8. Apolitical (8,0,3 or 5,0,3/8/9 in E6, E6a/b, E6c and no interest
##           in politics
##        9. NA (8/9,0,0 in E6, E6a/b, E6c)
DATA00$`Partisan strength` <- abs(as.numeric(DATA00$M000523)-4)## ^2
DATA00$`Partisan strength`[as.numeric(DATA00$M000523)>=8] <- NA
DATA00$`Partisan strength04` <- abs(as.numeric(DATA00$M045058x)-4)## ^2
DATA00$`Partisan strength2` <- abs(as.numeric(DATA00$M045058x)-4)## ^2

setwd("../../../data/ANES/anes_mergedfile_1992to1997_dta")

DATA92 <- read.dta("anes_mergedfile_1992to1997.dta")

DATA92$`Highest education` <- as.numeric(DATA92$V923908)-1
## VAR M923908    92PRE: Y3x. R EDUCAT
##              Y3, and Y3a-b were not asked of respondents on the short
##              form questionnaire  For panel short form respondents the
##              data for this variable was copied from the equivalent 1990
##              Post-election varialbe [although the data for the component
##              variable Y3, Y3a-b was not copied]
##              Built from Y3, Y3a-b
##                  00  Inap,4 in V3011
##                  01  8 grades or less (0-8 in Y3)
##                  02  9-11 grades, no further schooling (incl 12 yrs
##                       without diploma or equivalency) (9-12 in Y3 and 5
##                       in Y3a)
##                  03  High school diploma, or equivalency test (1 in Y3a)
##                  04  More than 12 years of schooling, no higher degree
##                       (13-17 in Y3 and 96 in Y3b)
##                  05  Junior or community college level degrees (AA
##                       degrees) (07 in Y3b)
##                  06  BA level degrees; 17+ years, no advanced degree (01
##                       in Y3b)
##                  07  Advanced degree, including LLB (02-06 in Y3b)
##                  98  DK (98 in Y3; 8 in Y3a; 98 in Y3b)
##                  99  NA (99 in Y3; 9-12 in Y3 and 9 in Y3a; 13-17 in Y3
##                       and 99 in Y3b)
DATA92$`Highest education`[DATA92$`Highest education`>=8|DATA92$`Highest education`==0] <- NA

DATA92$`Discusses politics (days in week)` <- as.numeric(DATA92$V925107)
## VAR M925107    92POST: A3b.
##              A3b.  How many days in the past week did you talk about
##              politics with family or friends?
##              NUMBER OF DAYS
##                  0.  NONE
##                  1.  ONE
##                  2.  TWO
##                  3.  THREE
##                  4.  FOUR
##                  5.  FIVE
##                  6.  SIX
##                  7.  SEVEN
##                  8.  DON'T KNOW
##                  9.  NA; Inap, no post-interview; 5, 8-9 in A3; 5, 9 in
##                      A3a
DATA92$`Discusses politics (days in week)`[!is.na(DATA92$`Discusses politics (days in week)`)&DATA92$`Discusses politics (days in week)`>=9] <- NA
DATA92$`Discusses politics (days in week)`[is.na(DATA92$`Discusses politics (days in week)`)&as.integer(DATA92$V925105)==2] <- 1
DATA92$`Discusses politics (days in week)`[is.na(DATA92$`Discusses politics (days in week)`)&as.integer(DATA92$V925105)==3] <- 0

DATA92$marital.status <- DATA92$V923904
## VAR M923904    92PRE: Y2. R MARITAL ST
##              Y2.  Are you married now and living with your
##              (husband/wife)-- or are you widowed, divorced, separated, or
##              have you never married?
##              ------------------------------------------------------------
##              NOTE:  Y2 was not asked of panel respondents interviewed
##              using the "short form" questionnaire, and their marital
##              status from 1990 was copied here.  For 22 cases the 1990
##              data were inconsistent with the Y21 CHECKPOINT in 1992,
##              alerting us to possible changes in marital status.  Each
##              case was reviewed and Y2 was corrected using information
##              from the cover sheet, the thumbnail sketch and other
##              variables.  Y2 then represents our best estimate of R's
##              PRESENT MARITAL STATUS.
##              If it is known that R is not currently married/partnered (1
##              in Y3c), but R refused to define specific status either as
##              never married, divorced, separated or widowed, then code 8
##              has been used.
##              If spouse is in nursing home or spouse works and lives in
##              another city, or if R's family is in the process of moving
##              and is temporarily split between two residences, code 1 is
##              used.
##                  0.  Inap, 4 in V3011
##                  1.  MARRIED AND LIVING WITH SPOUSE (OR SPOUSE IN
##                      SERVICE)
##                  2.  NEVER MARRIED
##                  3.  DIVORCED
##                  4.  SEPARATED
##                  5.  WIDOWED
##                  7.  PARTNERS NOT MARRIED (VOL.)
##                  8.  R not married/partnered (1 in Y3c), R refused to say
##                      whether never married, divorced, separated or
##                      widowed
##                  9.  NA
levels(DATA92$marital.status) <- c(NA, "Married or partnered","Never married","Divorced or separated","Divorced or separated","Widowed", "Married or partnered",NA,NA)
DATA92$`Married` <- as.integer(as.numeric(DATA92$V923904) == 2)

DATA92$`Age` <- DATA92$V923903
## 00.  NA; 4 in V3011
DATA92$`Age^2` <- DATA92$V923903^2


DATA92$`Interested in campaign` <- as.numeric(DATA92$V925102)-1
## VAR M925102    92POST: A1. INTERST CM
##              A1.  Some people don't pay much attention to political
##              campaigns.  How about you?  Would you say that you were very
##              much interested, somewhat interested, or not much interested
##              in following the political campaigns this year?
##                  0.  Inap, no post-interview 0 respondents
##                  1.  VERY MUCH INTERESTED
##                  3.  SOMEWHAT INTERESTED
##                  5.  NOT MUCH INTERESTED
##                  8.  DK 0 respondents
##                  9.  NA 0 respondents
DATA92$`Interested in campaign`[as.numeric(DATA92$`Interested in campaign`)==5] <- NA
DATA92$`Interested in campaign` <- abs(as.numeric(DATA92$`Interested in campaign`)-4)

DATA92$`Moved` <- with(DATA92, as.integer(V941423<=3|V941426<=3|V960712<=6|V960713<=6)) #M941423<=3
## VAR M941423    94PO: HOW LONG IN COMMUNITY (Y50.)
##         Q.Y50.   How long have you lived in your present
##         (CITY/TOWN/TOWNSHIP/COUNTY)?
##         If R gives fraction of year, drop the fraction and code the
##         actual number of years when greater than 2.
##         * 13-23 months and 2 years were coded 2 in Y50.  Y50a was
##         asked only of R's who answered 13-23.
##         Code actual number of years (01-89), except:
##             00.  LESS THAN 6 MONTHS
##             01.  6-12 MONTHS
##             02.  13-24 MONTHS; 2 YEARS *
##             89.  89 OR MORE
##             90.  ALL OF LIFE
##             98.  DK
##             99.  NA
##
## VAR M941426    94PO: HOW LONG LIVED IN RESIDNCE (Y51.)
##               COLUMNS 3337 - 3338
##               NUMERIC
##         Q.Y51.   How long have you lived in this
##         (house/condo/apartment)?
##         ----------------------------------------
##         If R gives fraction of year, drop the fraction and code the
##         actual number of years when greater than 2.
##         Code actual number of years (01-89), except:
##             00.  LESS THAN SIX MONTHS
##             01.  6-12 MONTHS; 1 YEAR
##             02.  13-24 MONTHS; 2 YEARS
##             03.  3 YEARS
##                  .
##                  .
##             89.  89 OR MORE
##             90.  ALL OF LIFE
##             98.  DK
##             99.  NA
##
## VAR M960712    COLUMNS 4987 - 4988
##          How long have you lived in your present (city/town/township/county)?
##          -----------------------------------------------------------
##               If R gave fraction of year, the fraction was dropped when
##               number of years was greater than 2.
##               Number of years coded, codes 3-88 and:
##                 00.   Less than 6 months
##                  1.   6-12 months; 1 year
##                  2.   13-24 months; 2 years
##                 89.   89 years or more
##                 90.   All of life
##                 98.   DK
##                 99.   NA
##
## VAR M960713    COLUMNS 4989 - 4990
##          How long have you lived in this (house/condo/apartment)?
##               If R gave fraction of year, the fraction was dropped when
##               number of years was greater than 2.
##               Number of years coded, codes 3-88 and:
##                 00.   Less than 6 months
##                  1.   6-12 months; 1 year
##                  2.   13-24 months; 2 years
##                 89.   89 years or more
##                 90.   All of life
##                 98.   DK
##                 99.   NA


DATA92$`Partisanship (left to right on 7-pt scale)` <- as.numeric(DATA92$V923634)
DATA92$`Partisanship (left to right on 7-pt scale)`[as.numeric(DATA92$V923634)>=8] <- NA
## VAR M923634    92PRE: K1z. PARTY ID
##              Code 8 (APOLITICAL) was used only if Respondent had a code
##              of 3 (NO PREFERENCE) in K1 and a code of 3, 8, or 9
##              (NEITHER, DK, OR NA) in K1c and also showed little or no
##              interest in politics in response to each of the following
##              questions: A1 (pre), A4 (pre), M4 (pre), F1 (post).
##              Respondents who showed interest in politics in response to
##              A1 (pre), A4 (pre), M4 (pre), or F1 (post) were recoded to
##              3.
##                  0.  STRONG DEMOCRAT (510)
##                  1.  WEAK DEMOCRAT (550, 580, 590)
##                  2.  INDEPENDENT-DEMOCRAT (205, 305, 405)
##                  3.  INDEPENDENT-INDEPENDENT (203, 208, 209)
##                  4.  INDEPENDENT-REPUBLICAN (201, 301, 401)
##                  5.  WEAK REPUBLICAN (150, 180, 190)
##                  6.  STRONG REPUBLICAN (110)
##                  7.  OTHER-MINOR PARTY, REFUSES TO SAY (403, 408, 409)
##                  8.  APOLITICAL (303, 308, 309)
##                  9.  NA (800, 900)
##
DATA92$`Partisan strength` <- abs(as.numeric(DATA92$V923634)-4)## ^2
DATA92$`Partisan strength`[as.numeric(DATA92$V923634)>=8] <- NA
DATA92$`Partisan strength96` <- abs(as.numeric(DATA92$V960420)-4)## ^2
DATA92$`Partisan strength2` <- abs(as.numeric(DATA92$V960420)-4)## ^2

DATA92$`Changed party` <- with(
    DATA92,
    abs(as.numeric(V923634) - as.numeric(V960420))>=4
    )
DATA92$`Changed party`[as.numeric(DATA92$V923634)>=8|as.numeric(DATA92$V923634)==4] <- NA
DATA92$`Changed party`[as.numeric(DATA92$V960420)>=8] <- NA

DATA92$`Changed partyB` <- with(
    DATA92,
    abs(as.numeric(V923634) - as.numeric(V960420))>=4
    |abs(as.numeric(V923634) - as.numeric(V960420))>=3&as.numeric(V923634)%in%c(3,5)
    |abs(as.numeric(V923634) - as.numeric(V960420))>=3&as.numeric(V960420)%in%c(3,5)
    )
DATA92$`Changed partyB`[as.numeric(DATA92$V923634)>=8|as.numeric(DATA92$V923634)==4] <- NA
DATA92$`Changed partyB`[as.numeric(DATA92$V960420)>=8] <- NA

DATA92$`Changed party3` <- with(
    DATA92,
    abs(as.numeric(V923634) - as.numeric(V960420))>=3
    )
DATA92$`Changed party3`[as.numeric(DATA92$V923634)>=8] <- NA
DATA92$`Changed party3`[as.numeric(DATA92$V960420)>=8] <- NA

DATA92$`Changed party2` <- with(
    DATA92,
    abs(as.numeric(V923634) - as.numeric(V960420))>=2
    )
DATA92$`Changed party2`[as.numeric(DATA92$V923634)>=8] <- NA
DATA92$`Changed party2`[as.numeric(DATA92$V960420)>=8] <- NA

DATA92$`Changed party1` <- with(
    DATA92,
    abs(as.numeric(V923634) - as.numeric(V960420))>=1
    )
DATA92$`Changed party1`[as.numeric(DATA92$V923634)>=8] <- NA
DATA92$`Changed party1`[as.numeric(DATA92$V960420)>=8] <- NA


## V937370
## M940655
## M952263A
## M960418
## M960420!?
## VAR M970106    COLUMNS 7325 - 7325
##      SUMMARY - PARTY ID
##              0       STRONG DEMOCRAT
##              1       WEAK DEMOCRAT
##              2       INDEPENDENT-DEMOCRAT
##              3       INDEPENDENT-INDEPENDENT
##              4       INDEPENDENT-REPUBLICAN
##              5       WEAK REPUBLICAN
##              6       STRONG REPUBLICAN
##              7       OTHER; MINOR PARTY; REFUSES TO SAY
##              8       APOLITICAL
##              9       NA


setwd("../../../data/ANES/anes_mergedfile_1972to1976_dta")

DATA72 <- read.dta("anes_mergedfile_1972to1976.dta")

DATA72$`Age` <- DATA72$V720294
DATA72$`Age`[DATA72$`Age`==0] <- NA
DATA72$`Age^2` <- DATA72$V720294^2
DATA72$`Age^2`[DATA72$`Age`==0] <- NA

DATA72$`Highest education` <- DATA72$V720300
levels(DATA72$`Highest education`) <- c(
    rep("1", 10),
    rep("2", 6),
    rep("3", 3),
    rep("4", 1),
    ## rep("5", 1),
    rep("6", 1),
    rep("7", 6),
    ## rep("8", 5),
    NA,
    NA
    )
## VAR 720300    NAME-1R'S EDUCATION
##                 **FORMS 1 AND 2**                      PRE-ELECTION QUESTION
##                 SUMMARY:  EDUCATION OF RESPONDENT
##                 K5.  HOW MANY GRADES OF SCHOOL DID YOU FINISH?
##                      (IF LESS THAN 12) DO YOU HAVE A HIGH SCHOOL EQUIVALENCY
##                      DIPLOMA OR CERTIFICATE?
##                 K5A.  HAVE YOU HAD ANY OTHER SCHOOLING?  (WHAT WAS THAT?)
##                       (ANY OTHER?)
##                 --IF R ATTENDED COLLEGE--
##                 K5B.  DO YOU HAVE A COLLEGE DEGREE?
##                 --IF R HAS A COLLEGE DEGREE--
##                 K5C.  WHAT DEGREE(S) HAVE YOU RECEIVED?
##                 ------------------------------------------------------------
##                 THE HIGHEST LEVEL OF EDUCATION ATTAINED WAS CODED.  THE
##                 "71" CODE (SOME COLLEGE) WAS USED IF THE INDIVIDUAL ATTENDED
##                 BUT DIDN'T FINISH A FOUR-YEAR COLLEGE WHICH GRANTS BACHELOR
##                 DEGREES OR IF (HE/SHE) TOOK A LIBERAL ARTS PROGRAM IN A
##                 JUNIOR COLLEGE.  "NORMAL SCHOOL" IS INCLUDED AS COLLEGE
##                 TRAINING.  BY "NON-COLLEGE" TRAINING IS MEANT VARIOUS VOCA-
##                 TIONAL AND TECHNICAL TRAINING PROGRAMS WHICH DO NOT GRANT
##                 ACADEMIC DEGREES:  E.G. BUSINESS "COLLEGE", BEAUTY
##                 "COLLEGE", NURSE TRAINING IN A HOSPITAL, ARMED SERVICE
##                 TRAINING PROGRAMS, ETC.
## 1
##                  8  11  1 GRADE OR LESS; NONE
##                  7  12  2 GRADES
##                 14  13  3 GRADES
##                 13  14  4 GRADES
##                 11  15  5 GRADES; "GRADE SCHOOL", NA EXACT LEVEL COMPLETED
##                 22  16  6 GRADES
##                 29  17  7 GRADES
##                  7  18  COMPLETED 7 GRADES OR LESS PLUS NON-COLLEGE TRAINING
##                103  21  8 GRADES
##                 15  22  COMPLETED 8 GRADES PLUS NON-COLLEGE TRAINING
## 2
##                 36  31  9 GRADES
##                 85  32  10 GRADES
##                 53  33  11 GRADES OR "HIGH SCHOOL", NA EXACT LEVEL
##                          COMPLETED
##                  6  41  9 GRADES PLUS NON-COLLEGE TRAINING
##                  8  42  10 GRADES PLUS NON-COLLEGE TRAINING
##                 18  43  11 GRADES PLUS NON-COLLEGE TRAINING; "HIGH SCHOOL" PLUS NON-COLLEGE TRAINING
##
## 3
##                 18  50  EQUIVALENCY <PRIORITY OVER CODES 11-43>
##                265  51  12 GRADES; COMPLETED HIGH SCHOOL
##                137  61  12 GRADES PLUS NON-COLLEGE TRAINING
## 4
##                243  71  SOME COLLEGE; AA; JUNIOR COLLEGE; "NORMAL SCHOOL"
## 6
##                169  81  BACHELOR'S DEGREE (EXCEPT CODE 84) (4 OR 5 YEARS
##                          UNDERGRADUATE STUDY) BS, BA, AB, AB IN TH, B ARCH,
##                          B CH E, BCL, BCS, BE, B ED, BFA, BJ, B LIT, BSA,
##                          BSC, BSED, BSFS, BS IN CE, BS IN CHE, BS IN ED,
##                          BS IN LS, JCB, LITB, PHB, SB, STB, BD
## 7
##                 31  82  MASTER'S DEGREE MS, MSC, MA MAT, MBA, BDS, MED,
##                          MFA, EDM, LLM, MPH, MPA, MS IN LS, MSW, MUSM, SM,
##                          STM, MMUS, MFS, MSLS
##                  6  83  PHD, LITD, SCD, DFA, DLIT, DPH, DPHIL, JSD, SJD
##                  8  84  LLB, JD
##                  6  85  MD, DDS, DVM, VS, DSC, DO (DOCTOR OF OSTEOPATHY)
##                  1  86  JCD, STD, THD
##                     87  HONORARY DEGREE LLD, DD, LHD
##                  1  98  DK
##                     99  NA
DATA72$`Highest education` <- as.numeric(DATA72$`Highest education`)

DATA72$`Married` <- as.integer(as.numeric(DATA72$V720295)==1)
DATA72$marital.status <- DATA72$V720295
levels(DATA72$marital.status) <- c("Married or partnered","Never married","Divorced or separated","Divorced or separated","Widowed", "Married or partnered",NA)

DATA72$`Interested in campaign` <- abs(as.numeric(DATA72$V720163)-4)
DATA72$`Interested in campaign`[as.numeric(DATA72$V720163)==5] <- NA
## VAR 720163    NAME-1R'S INTST-POLTL CAMPAIG
##                 **FORMS 1 AND 2**                      PRE-ELECTION QUESTION
##                 F1.  SOME PEOPLE DON'T PAY MUCH ATTENTION TO THE POLITICAL
##                      CAMPAIGNS  HOW ABOUT YOU, WOULD YOU SAY THAT YOU HAVE
##                      BEEN VERY MUCH INTERESTED, SOMEWHAT INTERESTED, OR NOT
##                      MUCH INTERESTED IN FOLLOWING THE POLITICAL CAMPAIGNS SO
##                      FAR THIS YEAR?
##                 ------------------------------------------------------------
##                485  1  VERY MUCH INTERESTED
##                553  3  SOMEWHAT INTERESTED
##                280  5  NOT MUCH INTERESTED
##                     8  DK
##                  2  9  NA
## DATA72$`Discusses politics (days in week)` <-

DATA72$`Partisanship (left to right on 7-pt scale)` <- as.numeric(DATA72$V720140)
DATA72$`Partisanship (left to right on 7-pt scale)`[as.numeric(DATA72$V720140)>=8] <- NA
DATA72$`Partisan strength` <- abs(as.numeric(DATA72$V720140)-4)## ^2
DATA72$`Partisan strength`[as.numeric(DATA72$V720140)>=8] <- NA
DATA72$`Partisan strength76` <- abs(as.numeric(DATA72$V763174)-4)## ^2
DATA72$`Partisan strength2` <- abs(as.numeric(DATA72$V763174)-4)## ^2

## VAR 742546    NAME-3HW LNG R LV THS HS/APT
##                 Q.S44.  HOW LONG HAVE YOU LIVED IN THIS HOUSE (APARTMENT)?
##                 ----------------------------------------------------------
##                 ACTUAL NUMBER OF YEARS WAS CODED
##                     01.  ONE YEAR OR LESS
##                           .
##                           .
##                     97.  97 YEARS OR MORE
##                     98.  DK
##                     99.  NA
##
## VAR 742543    NAME-3HOW LONG R IN THS CMNTY
##               COLUMNS 2507 - 2508
##               NUMERIC
##               MD=GE  98
##                 Q.S43.  HOW LONG HAVE YOU LIVED HERE IN (CITY/TOWN/COUNTY)?
##                 -----------------------------------------------------------
##                 THE ACTUAL NUMBER OF YEARS WAS CODED WITH HALF A YEAR OR
##                 MORE ROUNDED UPWARD.
##                     01.  ONE YEAR OR LESS
##                               .
##                               .
##                     97.  97 YEARS OR MORE
##                     98.  DK
##                     99.  NA
##
## VAR 763503    NAME-4HW LNG R LV THS HSE/APT
##               COLUMNS 3422 - 3423
##               NUMERIC
##               MD=GE  98
##                 Q.S47.  HOW LONG HAVE YOU LIVED IN THIS HOUSE (APARTMENT)?
##                 ----------------------------------------------------------
##                 ACTUAL NUMBER OF YEARS WAS CODED
##                     01.  ONE YEAR OR LESS
##                           .
##                           .
##                     97.  97 YEARS OR MORE
##                     98.  DK
##                     99.  NA
##
## VAR 763502    NAME-4HOW LONG R IN THS CMNTY
##               COLUMNS 3420 - 3421
##               NUMERIC
##               MD=GE  98
##                 Q.S46.  HOW LONG HAVE YOU LIVED HERE IN (CITY/TOWN/COUNTY)?
##                 -----------------------------------------------------------
##                 THE ACTUAL NUMBER OF YEARS WAS CODED WITH HALF A YEAR OR
##                 MORE ROUNDED UPWARD.
##                     00.  LESS THAN 6 MONTHS
##                           .
##                           .
##                     89.  89 YEARS OR MORE
##                     90.  "ALL OF LIFE" (REGARDLESS OF HOW MANY YEARS)
##                     98.  DK
##                     99.  NA
DATA72$`Moved` <- with(DATA72, as.integer(V742546<=2|742543<=2|V763503<=4|V763502<=4)) #763502 is coded differently (0 is less than 6 months) but should still be <=4

DATA72$`Changed party` <- with(DATA72, abs(as.numeric(V720140) - as.numeric(V763174))>=4)
DATA72$`Changed party`[as.numeric(DATA72$V720140)>=8|as.numeric(DATA72$V720140)==4] <- NA
DATA72$`Changed party`[as.numeric(DATA72$V763174)>=8] <- NA

DATA72$`Changed partyB` <- with(
    DATA72,
    abs(as.numeric(V720140) - as.numeric(V763174))>=4
    |abs(as.numeric(V720140) - as.numeric(V763174))>=3&as.numeric(V720140)%in%c(3,5)
    |abs(as.numeric(V720140) - as.numeric(V763174))>=3&as.numeric(V763174)%in%c(3,5)
    )
DATA72$`Changed partyB`[as.numeric(DATA72$V720140)>=8|as.numeric(DATA72$V720140)==4] <- NA
DATA72$`Changed partyB`[as.numeric(DATA72$V763174)>=8] <- NA

DATA72$`Changed party3` <- with(DATA72, abs(as.numeric(V720140) - as.numeric(V763174))>=3)
DATA72$`Changed party3`[as.numeric(DATA72$V720140)>=8] <- NA
DATA72$`Changed party3`[as.numeric(DATA72$V763174)>=8] <- NA

DATA72$`Changed party2` <- with(DATA72, abs(as.numeric(V720140) - as.numeric(V763174))>=2)
DATA72$`Changed party2`[as.numeric(DATA72$V720140)>=8] <- NA
DATA72$`Changed party2`[as.numeric(DATA72$V763174)>=8] <- NA

DATA72$`Changed party1` <- with(DATA72, abs(as.numeric(V720140) - as.numeric(V763174))>=1)
DATA72$`Changed party1`[as.numeric(DATA72$V720140)>=8] <- NA
DATA72$`Changed party1`[as.numeric(DATA72$V763174)>=8] <- NA


DATA56 <- read.dta("../anes_panel_1956to1960dta/anes_mergedfile_1956to1960.dta")

scaleIt <- function(x) {
    scale(x, center=median(x,na.rm=T))
}


DATA56$`Highest education` <- DATA56$V560181
levels(DATA56$`Highest education`) <- c("1","1","2","2","2","3","3","4","5",NA)
DATA56$`Highest education` <- as.numeric(DATA56$`Highest education`)
## VAR 560181    NAME-56EDUCATION OF RESPONDNT
##                  *** SECTION A:  1956 INTERVIEWING ***
##                  PD 29, 29A, 29B, AND 29C.  EDUCATION OF RESPONDENT
##                  ..................................................
## 1
##             19       0.  NONE
##            193       1.  SOME GRADE SCHOOL
## 2
##            209       2.  COMPLETED GRADE SCHOOL
##            263       3.  SOME HIGH SCHOOL
##             30       4.  INCOMPLETE HIGH SCHOOL PLUS OTHER NON-COLLEGE
##                          SCHOOLING
## 3
##            296       5.  COMPLETED HIGH SCHOOL
##            137       6.  COMPLETED HIGH SCHOOL PLUS OTHER NON-COLLEGE
##                          SCHOOLING
## 4
##            149       7.  SOME COLLEGE
## 5
##            114       8.  COMPLETED COLLEGE (HAS A DEGREE)
##            556       9.  NA
## DATA56$income <- scaleIt(as.numeric(DATA56$M560997))

## 560092 - when I got married, other personal

DATA56$`Married` <- as.integer(as.numeric(DATA56$V560177) == 2)
DATA56$`Never married` <- as.integer(as.numeric(DATA56$V560177) == 3)
DATA56$marital.status <- DATA56$V560177
levels(DATA56$marital.status) <- c(NA, "Married or partnered","Single","Divorced or separated","Divorced or separated","Widowed",NA,NA)

DATA56$`Age` <- DATA56$V560175
levels(DATA56$Age) <- c("19","23","29","40","49","60","72",NA)
DATA56$`Age` <- as.numeric(as.character(DATA56$`Age`))
DATA56$`Age^2` <- as.numeric(DATA56$Age)^2

DATA56$`Interested in campaign` <- (abs(as.numeric(DATA56$V560097)-4))
DATA56$`Interested in campaign`[as.numeric(DATA56$V560097)>=4] <- NA

DATA56$`Changed party` <- with(DATA56, abs(as.numeric(V560088) - as.numeric(V600657))>=4) #580360 no other questions? 560088
DATA56$`Changed party`[as.numeric(DATA56$V560088)%in%c(4,8:10)] <- NA
DATA56$`Changed party`[as.numeric(DATA56$V600657)%in%8:10] <- NA

DATA56$`Changed partyB` <- with(
    DATA56,
    abs(as.numeric(V560088) - as.numeric(V600657))>=4
    |abs(as.numeric(V560088) - as.numeric(V600657))>=3&as.numeric(V560088)%in%c(3,5)
    |abs(as.numeric(V560088) - as.numeric(V600657))>=3&as.numeric(V600657)%in%c(3,5)
    )
DATA56$`Changed partyB`[as.numeric(DATA56$V560088)%in%c(4,8:10)] <- NA
DATA56$`Changed partyB`[as.numeric(DATA56$V600657)%in%8:10] <- NA

DATA56$`Changed party3` <- with(DATA56, abs(as.numeric(V560088) - as.numeric(V600657))>=3)
DATA56$`Changed party3`[as.numeric(DATA56$V560088)%in%c(8:10)] <- NA
DATA56$`Changed party3`[as.numeric(DATA56$V600657)%in%8:10] <- NA

DATA56$`Changed party2` <- with(DATA56, abs(as.numeric(V560088) - as.numeric(V600657))>=2)
DATA56$`Changed party2`[as.numeric(DATA56$V560088)%in%c(8:10)] <- NA
DATA56$`Changed party2`[as.numeric(DATA56$V600657)%in%8:10] <- NA

DATA56$`Changed party1` <- with(DATA56, abs(as.numeric(V560088) - as.numeric(V600657))>=1)
DATA56$`Changed party1`[as.numeric(DATA56$V560088)%in%c(8:10)] <- NA
DATA56$`Changed party1`[as.numeric(DATA56$V600657)%in%8:10] <- NA

DATA56$`Moved` <- with(DATA56, as.integer(as.integer(V580527)<=3|as.integer(V600751)<=4)) # coded oddly, see summaries

DATA56$`Partisanship (left to right on 7-pt scale)` <- as.numeric(DATA56$V560088)
DATA56$`Partisanship (left to right on 7-pt scale)`[as.numeric(DATA56$V560088)>=8] <-NA
DATA56$`Partisan strength` <- abs(as.numeric(DATA56$V560088)-4)## ^2
DATA56$`Partisan strength`[as.numeric(DATA56$V560088)>=8] <- NA
DATA56$`Partisan strength2` <- abs(as.numeric(DATA56$V600657)-4)## ^2
DATA56$`Partisan strength2`[as.numeric(DATA56$V600657)>=8] <- NA





selectVars <- c("Changed party","Moved","Age","Age^2","Interested in campaign", "Highest education", "Married", "Partisanship (left to right on 7-pt scale)", "Partisan strength", "Partisan strength2","Changed party3","Changed party2","Changed party1","Changed partyB")

DATA <- rbind(subset(DATA00, Years == "3. 2000 - 2002 - 2004", select=selectVars),
subset(DATA92, VCASEID <=921125, select=selectVars),
subset(DATA72, as.numeric(V720140)<=7 & as.numeric(V763174)<=7 & as.numeric(V720140)!=4, select=selectVars),
              subset(DATA56, as.numeric(`Partisanship (left to right on 7-pt scale)`)<=7 & as.numeric(V600657)<=7 & as.numeric(`Partisanship (left to right on 7-pt scale)`)!=4, select=selectVars)
              )

DATA <- rbind(
    subset(DATA00, select=selectVars),
    subset(DATA92, select=selectVars),
    subset(DATA72, select=selectVars),
    subset(DATA56, select=selectVars)
              )



glmmod <- with(subset(DATA, !is.na(Moved)),
         glm(
             `Changed party`~
             + scaleIt(log(`Age`))
             + scaleIt(`Interested in campaign`)
             + scaleIt(`Highest education`)
             + abs(1-`Married`)
             + scaleIt(`Partisan strength`),
             family="binomial"
             )
         )

glmmod2 <- with(subset(DATA, !is.na(Moved)),
         glm(
             `Changed party`~
             `Moved`
             + scaleIt(log(`Age`))
             + scaleIt(`Interested in campaign`)
             + scaleIt(`Highest education`)
             + abs(1-`Married`)
             + scaleIt(`Partisan strength`),
             family="binomial"
             )
         )




lmmod <- with(subset(DATA, !is.na(Moved)),
         lm(
             `Changed party`~
             scaleIt(log(`Age`))
             + scaleIt(`Interested in campaign`)
             + scaleIt(`Highest education`)
             + abs(1-`Married`)
             + scaleIt(`Partisan strength`)
             )
         )

lmmod2 <- with(subset(DATA, !is.na(Moved)),
         lm(
             `Changed party`~
             `Moved`
             + scaleIt(log(`Age`))
             + scaleIt(`Interested in campaign`)
             + scaleIt(`Highest education`)
             + abs(1-`Married`)
             + scaleIt(`Partisan strength`)
             )
         )



stargazer(lmmod, glmmod, lmmod2, glmmod2, type = "latex", star.char="",report=('vcsp'),out="../../../tables/anes_models_with56_glm.tex", model.numbers=F, column.sep.width="0pt") #,font.size="scriptsize"


DATA$Age.log <- as.numeric(scaleIt(log(as.numeric(DATA$Age))))
DATA$`Highest education (scaled)` <- scaleIt((as.numeric(DATA$`Highest education`)))
DATA$Age.log.log <- as.numeric(scaleIt(log(log(DATA$Age)))) #?
DATA$Age <- as.numeric(scaleIt((as.numeric(DATA$Age))))

glmmodMediator <- with(subset(DATA, !is.na(Moved) & !is.na(`Changed party`)),
         glm(
             Moved ~
             Age + `Highest education (scaled)`,
             family="binomial"
             )
                       )


glmmodWithAge <- with(subset(DATA, !is.na(Moved) & !is.na(`Changed party`)),
         glm(
             `Changed party`~
             `Moved` + Age + `Highest education (scaled)`,
             family="binomial"
             )
         )

medmod <- mediate(glmmodMediator, glmmodWithAge, treat = "Age",mediator="Moved",covariates=c("Highest education (scaled)"),boot=T,boot.ci.type = "bca")
summary(medmod)

## glmmodMediatorNoEdu <- with(subset(DATA, !is.na(Moved) & !is.na(`Changed party`)),
##          glm(
##              Moved ~
##              Age.log,
##              family="binomial"
##              )
##          )

## glmmodWithAgeNoEdu <- with(subset(DATA, !is.na(Moved) & !is.na(`Changed party`)),
##          glm(
##              `Changed party`~
##              `Moved` + Age.log,
##              family="binomial"
##              )
##          )

## medmodNoEdu <- mediate(glmmodMediatorNoEdu, glmmodWithAgeNoEdu, treat = "Age.log",mediator="Moved")
## summary(medmodNoEdu)


glmmodNoControls <- with(DATA,
         glm(
             `Changed party`~
             `Moved` + Age.log,
             family="binomial"
             )
         )


preds.se <- predict(
    glmmodNoControls,
    newdata = data.frame(Moved = c(0,1), Age.log = 0),
    se.fit=T,
    type="link"
    )
critval <- 1.96 ## approx 95% CI
upr <- preds.se$fit + (critval * preds.se$se.fit)
lwr <- preds.se$fit - (critval * preds.se$se.fit)
fit <- preds.se$fit

fit2 <- glmmodNoControls$family$linkinv(fit)
upr2 <- glmmodNoControls$family$linkinv(upr)
lwr2 <- glmmodNoControls$family$linkinv(lwr)

library(ggplot2)

setwd("../../../figs/")

modNoControls <- with(DATA,
         glm(
             `Changed party`~
             `Moved`,
             family=binomial(link="log")
             )
         )
PartyChange4 <- list(
    ests=exp(coef(modNoControls)),
    cis=exp(confint(modNoControls))
    )

modNoControls3 <- with(DATA,
         glm(
             `Changed party3` ~
             `Moved`,
             family=binomial(link="log")
             )
         )
PartyChange3 <- list(
    ests=exp(coef(modNoControls3)),
    cis=exp(confint(modNoControls3))
    )

modNoControls2 <- with(DATA,
         glm(
             `Changed party2` ~
             `Moved`,
             family=binomial(link="log")
             )
         )
PartyChange2 <- list(
    ests=exp(coef(modNoControls2)),
    cis=exp(confint(modNoControls2))
    )

modNoControls1 <- with(DATA,
         glm(
             `Changed party1` ~
             `Moved`,
             family=binomial(link="log")
             )
         )
PartyChange1 <- list(
    ests=exp(coef(modNoControls1)),
    cis=exp(confint(modNoControls1))
    )



pdf("../../figs/predicted_prob_moving_no_controls_base.pdf",width=10,height=4)
par(mar=c(6,5,2,3),mfrow=c(1,2))
##
plot(
    1:4,
    rev(c(
        PartyChange4$est[2],
        PartyChange3$est[2],
        PartyChange2$est[2],
        PartyChange1$est[2]
        )),
    pch=16,
    ylim=c(0.4,2.5),
    xlim=c(0.5,4.5),
    col="purple3",
    bty="n",
    xaxt="n",
    ylab="Relative risk of party ID change",
    xlab="7 point scale change",
    cex.lab=1.3
    )
## abline(h=1,col="red",lty=2)
mtext("for movers vs. non-movers",side=2,padj=-3.5)
the.models <- rev(c("PartyChange4","PartyChange3","PartyChange2","PartyChange1"))
axis(1, at = 1:4, labels=paste(">= ",1:4,"",sep=""),tick=F)
## text(
##     1:4,
##     y=0.8,
##     labels=    rev(sprintf("%.2f",round(c(
##         PartyChange4$est[1],
##         PartyChange3$est[1],
##         PartyChange2$est[1],
##         PartyChange1$est[1]
##         ),2))),
##     cex=0.8,col="black"
##         )
## text(
##     1,
##     0.6,
##     labels="Non-mover\nchange proportion\n(baseline)",
##     cex=0.5,
##     col="black"
##     )
for (i in 1:4) {
    segments(
        x0=i,
        x1=i,
        y0=get(the.models[i])$cis[2,1],
        y1=get(the.models[i])$cis[2,2],
        col="purple3"
        )
}
abline(v=3.5,lty=2)
mtext("party-switching",side=3,at=4,cex=1)
mtext("(minimum)",side=1,cex=1,padj=7)
points(x=4,1, pch = 16)
text(x=4, 0.8, labels="partisan\nattachment", cex = 0.9)
##
plot(
    c(0),fit2[1],
    xaxt="n",xlab="",ylab="Party-switching rate",
    xlim=c(-0.4,1.4),ylim=c(0,0.1),
    pch=16,bty="n",cex=1.1,cex.lab=1.3
    )
points(
    c(1),fit2[2],
    xaxt="n",xlab="",ylab="Party-switching",
    xlim=c(-0.4,1.4),ylim=c(0,0.1),
    pch=16,bty="n",cex=1.1,cex.lab=1.3,col="purple3"
    )
for (i in 1:2) {
segments(x0=i-1,x1=i-1,y0=lwr2[i],y1=upr2[i],lwd=1.5,col=ifelse(i==1,"black","purple3"))
}
axis(side=1,labels=c("Non-movers","Movers"),at=c(0,1),line=NA,tick=F,cex=1.5)
points(x=1,fit2[1], pch = 16)
text(x=1, fit2[1]-0.01, labels="partisan\nattachment", cex = 0.9)
dev.off()

modNoControls <- with(DATA,
         glm(
             `Changed party`~
             `Moved` + scaleIt(`Highest education`) + scaleIt(Age),
             family=binomial(link="log")
             )
         )
PartyChange4 <- list(
    ests=exp(coef(modNoControls)),
    cis=exp(confint(modNoControls))
    )

modNoControls3 <- with(DATA,
         glm(
             `Changed party3` - `Changed party`~
             `Moved` + scaleIt(`Highest education`) + scaleIt(Age),
             family=binomial(link="log")
             )
         )
PartyChange3 <- list(
    ests=exp(coef(modNoControls3)),
    cis=exp(confint(modNoControls3))
    )

modNoControls2 <- with(DATA,
         glm(
             `Changed party2` - `Changed party3`~
             `Moved` + scaleIt(`Highest education`) + scaleIt(Age),
             family=binomial(link="log")
             )
         )
PartyChange2 <- list(
    ests=exp(coef(modNoControls2)),
    cis=exp(confint(modNoControls2))
    )

modNoControls1 <- with(DATA,
         glm(
             `Changed party1` - `Changed party2`~
             `Moved` + scaleIt(`Highest education`) + scaleIt(Age),
             family=binomial(link="log")
             )
         )
PartyChange1 <- list(
    ests=exp(coef(modNoControls1)),
    cis=exp(confint(modNoControls1))
    )


pdf("../../figs/predicted_prob_moving_no_controls_specificpointchange.pdf",width=5,height=4)
par(mar=c(6,5,2,3),mfrow=c(1,1))
plot(
    1:4,
    rev(c(
        PartyChange4$est[2],
        PartyChange3$est[2],
        PartyChange2$est[2],
        PartyChange1$est[2]
        )),
    pch=16,
    ylim=c(0.4,2.5),
    xlim=c(0.5,4.5),
    col="purple3",
    bty="n",
    xaxt="n",
    ylab="Relative party ID change",
    xlab="7 point scale change",
    cex.lab=1.3
    )
abline(h=1,col="red",lty=2)
mtext("(risk ratio: mover vs. non-mover)",side=2,padj=-3.5)
the.models <- rev(c("PartyChange4","PartyChange3","PartyChange2","PartyChange1"))
axis(1, at = 1:4, labels=1:4,tick=F)
text(
    1:4,
    y=0.8,
    labels=    rev(sprintf("%.2f",round(c(
        PartyChange4$est[1],
        PartyChange3$est[1],
        PartyChange2$est[1],
        PartyChange1$est[1]
        ),2))),
    cex=0.8,col="black"
        )
text(
    1,
    0.6,
    labels="Non-mover\nchange proportion\n(baseline)",
    cex=0.5,
    col="black"
    )
for (i in 1:4) {
    segments(
        x0=i,
        x1=i,
        y0=get(the.models[i])$cis[2,1],
        y1=get(the.models[i])$cis[2,2],
        col="purple3"
        )
}
abline(v=3.5,lty=2)
mtext("party-switching",side=3,at=4,cex=1)
mtext("(specific)",side=1,cex=1,padj=7)
dev.off()





modNoControls <- with(subset(DATA, !is.na(`Moved`)),
         lm(
             `Changed party`~
              Age.log + scaleIt(`Highest education`)
             )
                      )

mod <- with(DATA,
         lm(
             `Changed party`~
             `Moved` + Age.log + scaleIt(`Highest education`)
             )
         )

stargazer(modNoControls, mod, star.char="",report=('vcsp'),out="../../../tables/anes_models_with56_short.tex", font.size="scriptsize", model.numbers=F, column.sep.width="0pt")
